<?php

namespace app\common\model\mysql;

use app\common\business\DataAuth;
use think\Model;

class SysNotice extends Model
{
    /**
     * 自动生成写入时间
     * @var bool
     */
    protected $autoWriteTimestamp = true;

    /**
     * 新增前
     * @param $model
     * @return mixed|void
     */
    protected static function onBeforeInsert($model){
        $userData = (new DataAuth())->getUserDetails();
        $model->create_by = $userData['userName'];
    }

    /**
     * 更新前
     * @param $model
     * @return mixed|void
     */
    protected static function onBeforeUpdate($model)
    {
        $userData = (new DataAuth())->getUserDetails();
        $model->update_by = $userData['userName'];
    }

    /**
     * 查询通知公告列表
     * @param $where
     * @param $field
     * @return SysPost[]|array|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNormalNoticeList($where, $field,$limit,$page){
        $order = [
            "notice_id" =>"asc",
        ];
        $result = $this->where($where)->field($field)->order($order)
            ->paginate([
                'list_rows' => $limit,
                'page' => $page,
            ]);
        return $result;
    }

    /**
     * 根据$id获取岗位数据
     * @param $id
     * @return SysPost|array|false|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNoticeByNoticeId($id){
        if(empty($id)){
            return false;
        }
        $where = [
            "notice_id" =>$id,
        ];
        $result = $this->where($where)->find();
        return $result;
    }

    public function DelNoticeIds($ids){
        if(empty($ids)){
            return false;
        }
        $where = [
            ['notice_id', 'in', $ids]
        ];
        $result = $this->where($where)->delete();
        return $result;
    }
}
